// engineering/pages/thrashBinding/thrashBinding.js
var app = getApp();
var canUp=true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    deviceCode: '',//设备码
    thrashCode:'',//打水码
  },

  
  //输入设备编号
  deviceInput(e){
    this.setData({
      deviceCode:e.detail.value
    })
  },
  //打水码
  thrashInput(e){
    this.setData({
      thrashCode: e.detail.value
    })
  },
  //扫描设备编号
  deviceScan: function () {
    var that = this;
    // 只允许从相机扫码
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        console.log("扫码获取到的设备编号???", res.result);
        var newsp = res.result.split("=");
        if (newsp.length == 2 && newsp[1] != '') {
          that.setData({
            deviceCode: newsp[1]
          })
        } else {
          app.showToast('扫码失败请重试或者手动输入', "none", 2000, function () { });
        }

      }
    })
  },
  //扫描获取到的打水码
  thrashScan: function () {
    var that = this;
    // 只允许从相机扫码
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        console.log("扫码获取到的打水码>>>>", res.result);
        var getsp = res.result.split("=");
        if (getsp.length == 2 && getsp[1] != '') {
          that.setData({
            thrashCode: getsp[1]
          })
        } else {
          app.showToast('扫码失败请重试或者手动输入', "none", 2000, function () { });
        }

      }
    })
  },
  //提交表单数据
  upDataFun(){
    var that=this;
    var deviceCode = that.data.deviceCode;
    var thrashCode = that.data.thrashCode;
    if (deviceCode==''){
      app.showToast('设备号不能为空');return false;
    }
    if (thrashCode == '') {
      app.showToast('打水码不能为空'); return false;
    }
    if (canUp==false){
      app.showToast('请勿频繁操作'); return false;
    }
    canUp=false;
    app.ajax({
      url: "Manage/Equipment/bind_fetch_code",
      data: {
        equipment_number: deviceCode,
        fetch_code: thrashCode,
      },
      success: function (res) {
        if (res.data.code == 1000) {
          app.showToast('设备成功绑定成功');
          setTimeout(()=>{
            wx.navigateBack({
              delta: 1
            })
          },1000);
        } else {
          app.showToast(res.data.message);
        }
        setTimeout(()=>{
          canUp = true;
        },1500)

      }
    })
  }
})